﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Profile;

namespace DNA.Mvc
{
    public class UserProfile : ProfileBase
    {
        public static UserProfile GetProfile(string username)
        {
            return (UserProfile)ProfileBase.Create(username);
        }

        public string FirstName
        {
            get
            {
                return ValueGuard("FirstName","");
                //return (string)base.GetPropertyValue("FirstName");
            }
            set
            {
                base.SetPropertyValue("FirstName", value);
            }
        }

        public string LastName
        {
            get
            {
                return ValueGuard("LastName", "");
                //return (string)base.GetPropertyValue("LastName");
            }
            set
            {
                base.SetPropertyValue("LastName", value);
            }
        }

        public virtual string Gendar
        {
            get
            {
                return ValueGuard("Gendar", "P");
                //return (string)base.GetPropertyValue("Gendar");
            }
            set
            {
                base.SetPropertyValue("Gendar", value);
            }
        }

        public virtual string Address
        {
            get
            {
                return ValueGuard("Address", "");
                //return (string)base.GetPropertyValue("Address");
            }
            set
            {
                base.SetPropertyValue("Address", value);
            }
        }

        public virtual string Avatar
        {
            get
            {
                return ValueGuard("Avatar", "");
                //return (string)base.GetPropertyValue("Avatar");
            }
            set
            {
                base.SetPropertyValue("Avatar", value);
            }
        }

        public virtual DateTime Birthday
        {
            get
            {
                return ValueGuard("Birthday",DateTime.MinValue);
                //return (DateTime)base.GetPropertyValue("Birthday");
            }
            set
            {
                base.SetPropertyValue("Birthday", value);
            }
        }

        public virtual string Blog
        {
            get
            {
                return ValueGuard("Blog", "");
                //return (string)base.GetPropertyValue("Blog");
            }
            set
            {
                base.SetPropertyValue("Blog", value);
            }
        }

        public virtual string DisplayName
        {
            get
            {
                if (string.IsNullOrEmpty(FirstName))
                {
                    return FirstName +(!string.IsNullOrEmpty(LastName) ? " "+LastName : "");
                }
                return UserName;
            }
        }

        public virtual string Email
        {
            get
            {
                return ValueGuard("Email", "");
                //return (string)base.GetPropertyValue("Email");
            }
            set
            {
                base.SetPropertyValue("Email", value);
            }
        }

        public virtual string Facebook
        {
            get
            {
                return ValueGuard("Facebook", "");
                //return (string)base.GetPropertyValue("Facebook");
            }
            set
            {
                base.SetPropertyValue("Facebook", value);
            }
        }

        public virtual string Gravatar
        {
            get
            {
                return ValueGuard("Gravatar", "");
                //return (string)base.GetPropertyValue("Gravatar");
            }
            set
            {
                base.SetPropertyValue("Gravatar", value);
            }
        }

        public virtual string LinkedIn
        {
            get
            {
                return ValueGuard("LinkedIn", "");
                //return (string)base.GetPropertyValue("LinkedIn");
            }
            set
            {
                base.SetPropertyValue("LinkedIn", value);
            }
        }

        public virtual string GooglePlus
        {
            get
            {
                return ValueGuard("GooglePlus", "");
                //return (string)base.GetPropertyValue("GooglePlus");
            }
            set
            {
                base.SetPropertyValue("GooglePlus", value);
            }
        }

        public virtual string Language
        {
            get
            {
                return ValueGuard("Language", "");
                //return (string)base.GetPropertyValue("Language");
            }
            set
            {
                base.SetPropertyValue("Language", value);
            }
        }

        public virtual string Country
        {
            get
            {
                return ValueGuard("Country", "");
                //return (string)base.GetPropertyValue("Country");
            }
            set
            {
                base.SetPropertyValue("Country", value);
            }
        }

        public virtual string State
        {
            get
            {
                return ValueGuard("State", "");
                //return (string)base.GetPropertyValue("State");
            }
            set
            {
                base.SetPropertyValue("State", value);
            }
        }

        public virtual string City
        {
            get
            {
                return ValueGuard("City", "");
                //return (string)base.GetPropertyValue("City");
            }
            set
            {
                base.SetPropertyValue("City", value);
            }
        }

        public virtual string ZipCode
        {
            get
            {
                return ValueGuard("ZipCode", "");
                //return (string)base.GetPropertyValue("ZipCode");
            }
            set
            {
                base.SetPropertyValue("ZipCode", value);
            }
        }

        public virtual string Skype
        {
            get
            {
                return ValueGuard("Skype", "");
                //return (string)base.GetPropertyValue("Skype");
            }
            set
            {
                base.SetPropertyValue("Skype", value);
            }
        }

        public virtual string Mobile
        {
            get
            {
                return ValueGuard("Mobile", "");
                //return (string)base.GetPropertyValue("Mobile");
            }
            set
            {
                base.SetPropertyValue("Mobile", value);
            }
        }

        public virtual string MSN
        {
            get
            {
                return ValueGuard("MSN", "");
                //return (string)base.GetPropertyValue("MSN");
            }
            set
            {
                base.SetPropertyValue("MSN", value);
            }
        }

        public virtual string Phone
        {
            get
            {
                return ValueGuard("Phone", "");
                //return (string)base.GetPropertyValue("Phone");
            }
            set
            {
                base.SetPropertyValue("Phone", value);
            }
        }

        public virtual int Points
        {
            get
            {
                return ValueGuard("Points", 0);
                //return (int)base.GetPropertyValue("Points");
            }
            set
            {
                base.SetPropertyValue("Points", value);
            }
        }

        public virtual string Signature
        {
            get
            {
                return ValueGuard("Signature", "");
                //return (string)base.GetPropertyValue("Signature");
            }
            set
            {
                base.SetPropertyValue("Signature", value);
            }
        }

        public virtual string Theme
        {
            get
            {
                return ValueGuard("Theme", "default");
                //return (string)base.GetPropertyValue("Theme");
            }
            set
            {
                base.SetPropertyValue("Theme", value);
            }
        }

        public virtual string TimeZone
        {
            get
            {
                return ValueGuard("TimeZone", "");
                //return (string)base.GetPropertyValue("TimeZone");
            }
            set
            {
                base.SetPropertyValue("TimeZone", value);
            }
        }

        public virtual string Twitter
        {
            get
            {
                return ValueGuard("Twitter", "");
                //return (string)base.GetPropertyValue("Twitter");
            }
            set
            {
                base.SetPropertyValue("Twitter", value);
            }
        }

        public virtual string WebSite
        {
            get
            {
                return ValueGuard("WebSite", "");
                //return (string)base.GetPropertyValue("WebSite");
            }
            set
            {
                base.SetPropertyValue("WebSite", value);
            }
        }

        public virtual string Yahoo
        {
            get
            {
                return ValueGuard("Yahoo", "");
                //return (string)base.GetPropertyValue("Yahoo");
            }
            set
            {
                base.SetPropertyValue("Yahoo", value);
            }
        }

        private T ValueGuard<T>(string key, T defaultValue = default(T))
        { 
            var value=base.GetPropertyValue(key);
            if (value != null)
                return (T)value;
            else
                return defaultValue;
        }
    }
}